package com.takeaway.service.serviceimpl;

import com.takeaway.const_.Const;
import com.takeaway.dao.daointerface.CollectDao;
import com.takeaway.dao.daointerface.ShopDao;
import com.takeaway.dao.daolmpl.CollectDaoImpl;
import com.takeaway.dao.daolmpl.ShopDaoImpl;
import com.takeaway.dao.daolmpl.UserDaoImpl;
import com.takeaway.pojo.Collect;
import com.takeaway.pojo.Shop;
import com.takeaway.pojo.User;
import com.takeaway.service.serviceinterface.UserService;

import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.util.List;

/**
 * @author 后台赖骏
 */
public class UserServiceImpl implements UserService {
    private static final UserDaoImpl USER_DAO = new UserDaoImpl();
    private static final CollectDao COLLECT_DAO = new CollectDaoImpl();
    private static final ShopDao SHOP_DAO = new ShopDaoImpl();


    @Override
    public int addUser(String username,String pwd,int rightId)throws Exception{
        return USER_DAO.insert(new User(0,username,pwd,0,rightId,"default.jpg"));
    }

    @Override
    public User loginService(String username, String pwd) throws Exception{
        String[] checkField = {"username","pwd"};
        User user = USER_DAO.selectSingle(new User(), checkField, username, pwd);
        if (user.getId() == 0) {
            return null;
        }
        else {
            return user;
        }
    }

    @Override
    public int recharge(double money, User user) throws Exception {
        Double remain = user.getMoney()+money;
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        remain= Double.valueOf(decimalFormat.format(remain));
        user.setMoney(remain);
        return USER_DAO.update(user);
    }

    @Override
    public User checkUser(int id) throws Exception {
        String[] checkField = {Const.FIELD_ID};
        return USER_DAO.selectSingle(new User(),checkField,id);
    }
    @Override
    public User checkUser(String username) throws Exception {
        String[] checkField = {Const.FIELD_USERNAME};
        return USER_DAO.selectSingle(new User(),checkField,username);
    }


    @Override
    public int banUser(int userid) throws Exception {
        String[] checkField = {Const.FIELD_ID};
        int update = 0;
        User user = USER_DAO.selectSingle(new User(), checkField, userid);
        if (user.getRightId() == Const.USER_ID){
            user.setRightId(Const.BAN_USER_ID);
            update = USER_DAO.update(user);
        }
        else if (user.getRightId() == Const.BUSINESS_ID){
            user.setRightId(Const.BAN_BUSINESS_ID);
            update = USER_DAO.update(user);
            String[] checkField1 = {Const.FIELD_BUSINESS_ID};
            ShopDao shopDao = new ShopDaoImpl();
            Shop shop = shopDao.selectSingle(new Shop(), checkField1, user.getId());
            shop.setShopCode(Const.BAN_SHOP_ID);
            shopDao.update(shop);
        }
        return update;
    }

    @Override
    public int recoverUser(int userid) throws Exception {
        String[] checkField = {Const.FIELD_ID};
        int update = 0;
        User user = USER_DAO.selectSingle(new User(), checkField, userid);
        if (user.getRightId() == Const.BAN_USER_ID){
            user.setRightId(Const.USER_ID);
            update = USER_DAO.update(user);
        }
        else if (user.getRightId() == Const.BAN_BUSINESS_ID){
            user.setRightId(Const.BUSINESS_ID);
            update = USER_DAO.update(user);
            String[] checkField1 = {Const.FIELD_BUSINESS_ID};
            Shop shop = SHOP_DAO.selectSingle(new Shop(), checkField1, user.getId());
            shop.setShopCode(Const.SHOP_RUN_CODE);
            SHOP_DAO.update(shop);
        }
        return update;
    }

    @Override
    public List<User> checkAllUser() throws Exception{
        return USER_DAO.selectMore(User.class,new String[0]);
    }

    @Override
    public int addCollect(int shopId, int userId) throws Exception {
        String[] checkField = {Const.FIELD_USERID,Const.FIELD_SHOP_ID};
        Collect collect = COLLECT_DAO.selectSingle(new Collect(), checkField, userId, shopId);
        if (collect.getId()==0){
            COLLECT_DAO.selectSingle(new Collect(),checkField,userId,shopId);
            return COLLECT_DAO.insert(new Collect(userId, shopId));
        }
        return 0;
    }

    @Override
    public int deleteCollect(int shopId) throws Exception {
        String[] checkField ={Const.FIELD_SHOP_ID};
        return COLLECT_DAO.delete(Collect.class,checkField,shopId);
    }

    @Override
    public int banUserLogin(int userid) throws Exception {
        String[] checkField = {Const.FIELD_ID};
        User user = USER_DAO.selectSingle(new User(), checkField, userid);
        if(user.getRightId()<Const.MANAGER_ID){
            Timestamp newBanTime = new Timestamp(System.currentTimeMillis()+60*1000);
            user.setBanTime(newBanTime);
            return USER_DAO.update(user);
        }
        return 0;
    }
    @Override
    public int giveUserLogin(int userid) throws Exception {
        String[] checkField = {Const.FIELD_ID};
        User user = USER_DAO.selectSingle(new User(), checkField, userid);
        if(user.getRightId()<Const.MANAGER_ID){
            Timestamp newBanTime = new Timestamp(System.currentTimeMillis());
            user.setBanTime(newBanTime);
            return USER_DAO.update(user);
        }
        return 0;
    }

    @Override
    public User updateUserImg(int id, String fileName) throws Exception {
        String[] checkField = {Const.FIELD_ID};
        User user = USER_DAO.selectSingle(new User(), checkField, id);
        user.setPhoto(fileName);
                USER_DAO.update(user);
        return user;
    }
}
